package com.jadcon.bedbreakfast.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class CustomerValidator implements Validator {
	
	@Autowired 
	CustomerRepository customerRepository;
	
	@SuppressWarnings("unchecked")
	public boolean supports(Class clazz) {
		return Customer.class.equals(clazz);
	}

	public void validate(Object object, Errors e) {
		Customer customer = (Customer) object;
		String name = customer.getName();
		String address1 = customer.getAddress1();
		String city = customer.getCity();
		String state = customer.getState();
		int zip = customer.getZip();
		String email = customer.getEmail();
	
		if (name == null || "".equals(name)) {
			e.rejectValue("name", "name.empty");
		}
		else if (address1 == null || "".equals(address1)) {
			e.rejectValue("address1", "address1.empty");
		}
		else if (city == null || "".equals(city)) {
			e.rejectValue("city", "city.empty");
		}
		else if (state == null || "".equals(state)) {
			e.rejectValue("state", "state.empty");
		}
		else if (!(zip>=00000 && zip<=99999)) {
			e.rejectValue("zip", "zip.invalid");
		}
		else if (email == null || "".equals(email)){
	        e.rejectValue("email", "email.empty");
	    }		
	}
}
